package com.vtc.session9;

import java.util.ArrayList;
import java.util.List;

import android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GridViewAdapter extends BaseAdapter {

	private Context context;
	
	private List<Image> listImg = new ArrayList<Image>();
	public GridViewAdapter(Context context){
		this.context = context;
		
		listImg.add(new Image("Anh 1", com.vtc.R.drawable.sample_0, "Mo ta j o day"));
		listImg.add(new Image("Anh 2", com.vtc.R.drawable.sample_1, "Mo ta j o day"));
		listImg.add(new Image("Anh 3", com.vtc.R.drawable.sample_2, "Mo ta j o day"));
		listImg.add(new Image("Anh 4", com.vtc.R.drawable.sample_3, "Mo ta j o day"));
		listImg.add(new Image("Anh 5", com.vtc.R.drawable.sample_4, "Mo ta j o day"));
		listImg.add(new Image("Anh 6", com.vtc.R.drawable.sample_5, "Mo ta j o day"));
		listImg.add(new Image("Anh 7", com.vtc.R.drawable.sample_6, "Mo ta j o day"));
		listImg.add(new Image("Anh 8", com.vtc.R.drawable.sample_7, "Mo ta j o day"));
		listImg.add(new Image("Anh 9", com.vtc.R.drawable.sample_2, "Mo ta j o day"));
		listImg.add(new Image("Anh 10", com.vtc.R.drawable.sample_3, "Mo ta j o day"));
		listImg.add(new Image("Anh 11", com.vtc.R.drawable.sample_4, "Mo ta j o day"));
		listImg.add(new Image("Anh 12", com.vtc.R.drawable.sample_5, "Mo ta j o day"));
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listImg.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		LinearLayout layout=null;
		if(view==null){
			LayoutInflater inflat =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			layout = (LinearLayout) inflat.inflate(com.vtc.R.layout.gridview_item, null);
			ImageView imgView = (ImageView) layout.findViewById(com.vtc.R.id.gridview_img);
			TextView titleView = (TextView) layout.findViewById(com.vtc.R.id.gridview_title);
			
			Image img = listImg.get(position);
			
			imgView.setImageResource(img.getPath());
			imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			titleView.setText(img.getTitle());
		}
		else{
			layout = (LinearLayout) view;
			ImageView imgView = (ImageView) layout.findViewById(com.vtc.R.id.gridview_img);
			TextView titleView = (TextView) layout.findViewById(com.vtc.R.id.gridview_title);
			
			Image img = listImg.get(position);			
			imgView.setImageResource(img.getPath());
			imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			titleView.setText(img.getTitle());
		}
		return layout;
	}

}
